package com.ytjj.qmyx.supplychain.api.ceb.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.supplychain.api.ceb.model.request.LINKOrderCallbackRequest;
import com.ytjj.qmyx.supplychain.common.model.request.BankerOrderExpressRequest;
import com.ytjj.qmyx.supplychain.common.model.request.link.back.TmsOrderBack;
import com.ytjj.qmyx.supplychain.mapper.BankerOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/link/order")
public class LINKOrderController {
    @Resource
    private BankerOrderMapper bankerOrderMapper;

    /**
     * link 回调接口-快递单号信息
     * @return
     */
    @PostMapping("/callback/express")
    public String receiveOrderExpressCallback(@RequestParam Map<String, String> request) {//@RequestBody LINKOrderCallbackRequestOut request
        log.info("===link订单出库及包裹信息回传==={}", request);

        JSONObject result = new JSONObject();
        result.put("success", true);
        result.put("errorCode", "200");
        result.put("errorMsg", "成功");
        LINKOrderCallbackRequest params = JSONObject.parseObject(request.get("logistics_interface"), LINKOrderCallbackRequest.class);

        String childOrderNo = params.getOrderCode();
        List<TmsOrderBack> tmsOrders = params.getTmsOrders();
        if (!CollectionUtils.isEmpty(tmsOrders)) {
            TmsOrderBack tmsOrderBack = tmsOrders.get(0);
            String tmsCode = tmsOrderBack.getTmsCode();//快递公司编码
            String tmsName = tmsOrderBack.getTmsName();//快递格式名称
            String tmsOrderCode = tmsOrderBack.getTmsOrderCode();//运单编码
            String packageCode = tmsOrderBack.getPackageCode();//包裹号
            log.info("===abroad order exprss info==={},{},{},{},{}", childOrderNo, tmsCode, tmsName, tmsOrderCode, packageCode);

            BankerOrderExpressRequest obj = new BankerOrderExpressRequest();
            obj.setChildOrderNo(childOrderNo);
            obj.setExpressNo(tmsOrderCode);
            String companyCode = expressCodeTransfer(tmsCode);
            obj.setExpressCode(companyCode);
            log.info("===更新wms订单发货信息==={}", JSONObject.toJSONString(obj));
            int num = bankerOrderMapper.updateBankerOrdersBySingle(obj);
            if (num > 0) {
                return result.toJSONString();
            }
        }
        result.put("success", false);
        result.put("errorCode", "500");
        result.put("errorMsg", "更新失败");
        return result.toJSONString();
    }

    private String expressCodeTransfer(String key) {
        Map<String, String> map = new HashMap();
        map.put("DBKD", "debangwuliu");
        map.put("EMS", "ems");
        map.put("EYB", "EYB");//EMS经济快递
        map.put("FAST", "FAST");//快捷快递
        map.put("GTO", "guotongkuaidi");
        map.put("POSTB", "youzhengbk");
        map.put("QFKD", "quanfengkuaidi");
        map.put("HTKY", "huitongkuaidi");
        map.put("SF", "shunfeng");
        map.put("STO", "shentong");
        map.put("TTKDEX", "tiantian");
        map.put("UC", "youshuwuliu");
        map.put("YTO", "yuantong");
        map.put("YUNDA", "yunda");
        map.put("ZJS", "zhaijisong");
        map.put("ZTO", "zhongtong");

        return null == map.get(key) ? key : map.get(key);
    }
}
